跳到主要内容

布局脚本(Layout Scripts)

布局脚本(Layout Scripts)扩展了 节点脚本(Node Scripts) 的能力,使你可以程序化控制布局组件。

它适合实现瀑布流、滚动区域、动态间距等自定义布局行为。

示例(Examples)

将布局脚本添加到布局

  1. 在场景中添加 布局(Layout)
  2. 创建新脚本 并选择 布局(Layout) 类型
  3. 将脚本作为布局子节点添加

生命周期(Lifecycle)

布局脚本新增两个生命周期方法:

  • measure(self): Vec2D(可选)
  • resize(self, size: Vec2D)(必需)

measure

measure 用于向布局系统声明理想尺寸。

仅当 Fit 类型为 Hug 时该值会生效(除非被其他 Fit 规则覆盖)。

function measure(self: MyLayout): Vec2D
-- Always declare that this layout would like to be 100×100
return Vec2D.xy(100, 100)
end

resize

每当布局尺寸变化(来自父级或 measure 结果)时触发 resize

可在此更新子节点位置、重新排版、响应容器变化。

-- Called whenever the Layout is resized.
function resize(self: MyLayout, size: Vec2D)
print("New size:")
print("x:", size.x)
print("y:", size.y)
end